<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="操作类型" prop="noteType">
        <el-select style="width: 200px;" v-model="queryParams.noteType" placeholder="请选择操作类型" clearable>
          <el-option v-for="dict in note_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="管家婆项目" prop="housekeeperName">
        <el-input v-model="queryParams.housekeeperName" placeholder="请输入管家婆项目" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="操作人" prop="drawer">
        <el-input v-model="queryParams.drawer" placeholder="请输入操作人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="24">
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="outPromissory"  :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                            开据期票￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="outRedeem" :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                          开据承兑￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="outTotal" :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                          开据总额￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
    </el-row>

    <el-row :gutter="24">
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="acceptPromissory"  :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                            收到期票￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="acceptRedeem" :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                          收到承兑￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
        <el-col :span="8" :class="userStore.isMobile ? 'mobile-span-24' : ''">
            <div class="statistic-card">
                <el-statistic :value="acceptTotal" :formatter="(value) => value.toFixed(2)">
                    <template #title>
                        <div style="display: inline-flex; align-items: center;font-size: 16px;">
                          收到总额￥
                        </div>
                    </template>
                </el-statistic>
            </div>
        </el-col>
    </el-row>


    <!-- <el-row>
    <el-col :span="6">
      <el-statistic title="Daily active users" :value="268500" />
    </el-col>
    <el-col :span="6">
      <el-statistic :value="138">
        <template #title>
          <div style="display: inline-flex; align-items: center">
            Ratio of men to women
            <el-icon style="margin-left: 4px" :size="12">
              <Male />
            </el-icon>
          </div>
        </template>
        <template #suffix>/100</template>
      </el-statistic>
    </el-col>
    <el-col :span="6">
      <el-statistic title="Total Transactions" :value="outputValue" />
    </el-col>
    <el-col :span="6">
      <el-statistic title="Feedback number" :value="562">
        <template #suffix>
          <el-icon style="vertical-align: -0.125em">
            <ChatLineRound />
          </el-icon>
        </template>
      </el-statistic>
    </el-col>
  </el-row> -->

    <el-table v-loading="loading" border :row-class-name="tableRowClassName" :data="promissoryList" @selection-change="handleSelectionChange">
      <el-table-column label="出票日期" align="center" prop="outDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.outDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="到期日期" align="center" prop="endDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>


      <el-table-column label="操作类型" width="100" show-overflow-tooltip  align="center" prop="noteType">
        <template #default="scope">
          <dict-tag :options="note_type" :value="scope.row.noteType" />
        </template>
      </el-table-column>
      <el-table-column label="付款单位" width="200" show-overflow-tooltip align="center" prop="payer" />
      <el-table-column label="收款单位" width="200" show-overflow-tooltip align="center" prop="payee" />
      <el-table-column label="金额"   width="100" show-overflow-tooltip  align="center" prop="money" />
      <el-table-column label="管家婆项目" width="250" show-overflow-tooltip align="center" prop="housekeeperName" />
      <el-table-column label="操作人" align="center" prop="drawer" />
      <el-table-column label="票据单号"  width="250" show-overflow-tooltip  align="center" prop="ticketNumber" />
      <el-table-column label="到期时间" align="center"   width="100" show-overflow-tooltip   prop="endDate" />
      <el-table-column label="备注" align="center" width="100" show-overflow-tooltip  prop="remark" />
      <el-table-column label="操作" fixed="right" :width="userStore.isMobile ? 120 : 220"  align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" :disabled="scope.row.status == '1'" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改期票承兑信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>

      <el-form ref="promissoryRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="操作类型" prop="noteType">
          <el-select v-model="form.noteType" placeholder="请选择操作类型">
            <el-option v-for="dict in note_type"  :key="dict.value"  :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="管家婆项目" prop="housekeeperProject">
          <el-select v-model="form.housekeeperProject" filterable clearable remote reserve-keyword placeholder="请输入关键字"
            :remote-method="selectCompany" :loading="loadingQuery" style="width: 240px" @change="setHousekeeperName">
            <el-option v-for="item in companyList" :key="item.id" :label="item.companyName" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="出票日期" prop="outDate">
          <el-date-picker clearable v-model="form.outDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择出票日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="到期日期" prop="outDate">
          <el-date-picker clearable v-model="form.endDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择出票日期">
          </el-date-picker>
        </el-form-item>


        <el-form-item label="付款单位" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="收款单位" prop="payee">
          <el-input v-model="form.payee" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="票据单号" prop="ticketNumber">
          <el-input v-model="form.ticketNumber" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

        <!-- 数据统计对话框 -->
      <el-dialog title="数据统计" v-model="openData" fullscreen append-to-body>
        <promissoryData v-if="openData"></promissoryData>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Promissory">
import { listPromissory, getResult,getPromissory, delPromissory, addPromissory, updatePromissory } from "@/api/project/promissory";
import { listCompany } from "@/api/project/project";
import promissoryData from "./promissoryData.vue"
import { useTransition } from '@vueuse/core'
const { proxy } = getCurrentInstance();
const { note_type } = proxy.useDict('note_type');

const promissoryList = ref([]);
const open = ref(false);
const openData = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const companyList = ref([]);
const loadingQuery = ref(false)
const outPromissory = ref(0)
const outRedeem = ref(0)
const outTotal = ref(0)
const acceptPromissory = ref(0)
const acceptRedeem = ref(0)
const acceptTotal = ref(0)
const acceptTotalSource = ref(0)

import { ChatLineRound, Male } from '@element-plus/icons-vue'

const source = ref(0)
const outputValue = useTransition(source, {
  duration: 1500,
})
source.value = 3072000
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    outDate: null,
    endDate: null,
    payee: null,
    payer: null,
    money: null,
    housekeeperProject: null,
    housekeeperName: null,
    drawer: null,
    drawerId: null,
    ticketNumber: null,
    companyName: null,
    noteType: null,
    noteName: null
  },
  rules: {
    noteType: [{ required: true, message: "请选择操作类型", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询期票承兑信息列表 */
function getList() {
  loading.value = true;
  listPromissory(queryParams.value).then(response => {
    promissoryList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

  getResult(queryParams.value).then(response => {
    outPromissory.value = response.outPromissory
    outRedeem.value = response.outRedeem
    outTotal.value = response.outTotal
    acceptPromissory.value = response.acceptPromissory
    acceptRedeem.value = response.acceptRedeem
    acceptTotal.value = response.acceptTotal
    acceptTotalSource.value = response.acceptTotal
  });

}

function selectCompany(query) {
  console.log("queryquery", query);
  loadingQuery.value = true
  let params = {
    companyName: query
  }
  listCompany(params).then((response) => {
    companyList.value = response.data
    loadingQuery.value = false
  });
}

const setHousekeeperName = (id) => {

  if (id == undefined) {
    form.value.housekeeperName = '';
    return
  }
  const filteredObjects = companyList.value.find((obj) => obj.id === id);
  form.value.housekeeperName = '';
  form.value.housekeeperName = filteredObjects?.companyName || '';
}

const tableRowClassName = ({ row }) => {
  if (row.status === '1') {
    return 'gray-row';
  } else if (row.status === '2') {
    return 'warning-row';
  } else if (row.status === '3') {
    return 'success-row';
  }
  return '';
};

// 取消按钮
function cancel() {
  open.value = false;
  openData.value = false
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    outDate: null,
    endDate: null,
    payee: null,
    payer: null,
    money: null,
    housekeeperProject: null,
    housekeeperName: null,
    drawer: null,
    drawerId: null,
    ticketNumber: null,
    companyName: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    loginIp: null,
    loginDate: null,
    noteType: null,
    noteName: null
  };
  proxy.resetForm("promissoryRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加期票承兑信息";
}

/** 新增按钮操作 */
function handleData() {
  openData.value = true;
}



/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPromissory(_id).then(response => {
    form.value = response.data;
    if (form.value.housekeeperName) {
      selectCompany(form.value.housekeeperName)
    }
    open.value = true;
    title.value = "修改期票承兑信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["promissoryRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePromissory(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPromissory(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除期票承兑信息编号为"' + _ids + '"的数据项？').then(function () {
    return delPromissory(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('project/promissory/export', {
    ...queryParams.value
  }, `promissory_${new Date().getTime()}.xlsx`)
}

getList();


import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
</script>





<style scoped>
:global(h2#card-usage ~ .example .example-showcase) {
    background-color: var(--el-fill-color) !important;
}


.el-statistic {
    --el-statistic-content-font-size: 28px;
}

/* .statistic-card {
    height: 100%;
    padding: 10px;
    border-radius: 4px;
    background-color: var(--el-bg-color-overlay);
} */

.statistic-card {
    height: 100%;
    padding-bottom: 15px;
    padding-left: 10vw;
    border-radius: 4px;
    background-color: var(--el-bg-color-overlay);
}

.statistic-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    font-size: 12px;
    color: var(--el-text-color-regular);
    margin-top: 16px;
}

.statistic-footer .footer-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.statistic-footer .footer-item span:last-child {
    display: inline-flex;
    align-items: center;
    margin-left: 4px;
}

.green {
    color: var(--el-color-success);
}

.red {
    color: var(--el-color-error);
}

/* 

:deep(.el-table .warning-row) {
    --el-table-tr-bg-color: var(--el-color-warning-light-9);
} */

/* 悬停效果 */
/* :deep(.el-table .warning-row:hover td) {
    --el-table-tr-bg-color: var(--el-color-warning-light-8);
    background-color: var(--el-table-tr-bg-color);
} */

/* 如果需要更醒目的边框样式 */
/* :deep(.el-table .warning-row td) {
    border-bottom: 1px solid var(--el-color-warning-light-5);
    border-top: 1px solid var(--el-color-warning-light-5);
}

:deep(.el-table .warning-row td:first-child) {
    border-left: 1px solid var(--el-color-warning-light-5);
}

:deep(.el-table .warning-row td:last-child) {
    border-right: 1px solid var(--el-color-warning-light-5);
} */



/* 如果需要更醒目的边框样式 */
/* :deep(.el-table .success-row td) {
    border-bottom: 1px solid var(--el-color-warning-light-5);
    border-top: 1px solid var(--el-color-warning-light-5);
}

:deep(.el-table .success-row td:first-child) {
    border-left: 1px solid var(--el-color-warning-light-5);
}

:deep(.el-table .success-rowtd:last-child) {
    border-right: 1px solid var(--el-color-warning-light-5);
} */
</style>


<style>
.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-light-9);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-light-9);
}

/* 新增：浅灰色行样式 */
.el-table .gray-row {
  --el-table-tr-bg-color: #dfe1e4; /* Element Plus 默认的浅灰背景色之一 */
}
.el-table .gray2-row {
  --el-table-tr-bg-color: #f06578; /* Element Plus 默认的浅灰背景色之一 */
}

.mobile-span-24 {
  width: 100%;
  max-width: 100%;
  flex: 0 0 100%;
}
</style>